package cn.juque.jdfsystem.service.impl;

import cn.hutool.core.text.CharSequenceUtil;
import cn.juque.common.base.PageInfo;
import cn.juque.jdfsystem.cache.ConfigInfoCache;
import cn.juque.jdfsystem.entity.FileInfo;
import cn.juque.jdfsystem.entity.LogInfo;
import cn.juque.jdfsystem.enums.ConfigCodeEnum;
import cn.juque.jdfsystem.enums.LogOperateTypeEnum;
import cn.juque.jdfsystem.service.ILogInfoService;
import cn.juque.lucenecandy.core.base.BaseEntity;
import cn.juque.lucenecandy.core.base.IdQueryWrapperBuilder;
import cn.juque.lucenecandy.core.base.QueryWrapperBuilder;
import cn.juque.lucenecandy.core.enums.MatchTypeEnum;
import cn.juque.lucenecandy.helper.IndexHelper;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.SortField;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author JUQUE
 * @version 1.0.0
 * <ul>
 *  <li>simple-generator</li>
 *  <li>文件操作日志</li>
 * </ul>
 * @date 2023-01-10 18:21:44
 **/
@Service("logInfoService")
public class LogInfoServiceImpl implements ILogInfoService {

    @Resource
    private IndexHelper indexHelper;

    @Resource
    private ConfigInfoCache configInfoCache;

    /**
     * 分页查询
     *
     * @param pageInfo pageInfo
     * @param logInfo  logInfo
     * @return DataGrid
     */
    @Override
    public List<LogInfo> listInfoWithPage(PageInfo pageInfo, LogInfo logInfo) {
        QueryWrapperBuilder<LogInfo> queryWrapperBuilder = this.buildQuery(logInfo);
        queryWrapperBuilder.pageInfo(pageInfo).addSort(BaseEntity::getCreateTime, SortField.Type.LONG, true);
        return this.indexHelper.searchByPage(queryWrapperBuilder.build());
    }

    /**
     * 查询
     *
     * @param logInfo logInfo
     * @return List
     */
    @Override
    public List<LogInfo> listInfoWithoutPage(LogInfo logInfo) {
        QueryWrapperBuilder<LogInfo> queryWrapperBuilder = this.buildQuery(logInfo);
        queryWrapperBuilder.addSort(BaseEntity::getCreateTime, SortField.Type.LONG, true);
        return this.indexHelper.search(queryWrapperBuilder.build());
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return LogInfo
     */
    @Override
    public LogInfo detail(String id) {
        IdQueryWrapperBuilder<LogInfo> idQueryWrapperBuilder = new IdQueryWrapperBuilder<>(LogInfo.class, id);
        return this.indexHelper.searchById(idQueryWrapperBuilder.build());
    }

    /**
     * 保存
     *
     * @param fileInfo        fileInfo
     * @param operateTypeEnum LogOperateTypeEnum
     */
    @Override
    public void saveInfo(FileInfo fileInfo, LogOperateTypeEnum operateTypeEnum) {
        LogInfo logInfo = new LogInfo();
        logInfo.setFileId(fileInfo.getId());
        logInfo.setFileName(fileInfo.getFileName());
        logInfo.setOperateType(operateTypeEnum.getCode());
        String nodeName = this.configInfoCache.getValue(ConfigCodeEnum.NODE_NAME.getCode());
        String ip = this.configInfoCache.getValue(ConfigCodeEnum.BIND_IP.getCode());
        logInfo.setOperateNode(nodeName);
        logInfo.setOperateIp(ip);
        this.indexHelper.addDocument(logInfo);
    }

    /**
     * 构建查询条件
     *
     * @param logInfo 参数
     * @return BooleanQuery.Builder
     */
    private QueryWrapperBuilder<LogInfo> buildQuery(LogInfo logInfo) {
        QueryWrapperBuilder<LogInfo> queryWrapperBuilder = new QueryWrapperBuilder<>(LogInfo.class);
        if (CharSequenceUtil.isNotEmpty(logInfo.getFileId())) {
            queryWrapperBuilder.matchStr(LogInfo::getFileId, logInfo.getFileId(), MatchTypeEnum.TERM, BooleanClause.Occur.MUST);
        }
        if (CharSequenceUtil.isNotEmpty(logInfo.getFileName())) {
            queryWrapperBuilder.matchStr(LogInfo::getFileName, logInfo.getFileName(), MatchTypeEnum.ALL_LIKE, BooleanClause.Occur.MUST);
        }
        if (CharSequenceUtil.isNotEmpty(logInfo.getOperateNode())) {
            queryWrapperBuilder.matchStr(LogInfo::getOperateNode, logInfo.getOperateNode(), MatchTypeEnum.TERM, BooleanClause.Occur.MUST);
        }
        return queryWrapperBuilder;
    }
}
